﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet2
    {
        private void Sheet2_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet2_Shutdown(object sender, System.EventArgs e)
        {
        }
        
        #region VSTO で生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet2_Startup);
            this.Shutdown += new System.EventHandler(Sheet2_Shutdown);
        }

        #endregion

        private  const int POS_ContestCol = 2;
        private  const int POS_ContestRow = 1;
        private  const int POS_GameCol = 2;
        private  const int POS_GameRow = 2;

        private  const int POS_RoundCol = 4;
        private  const int POS_Round1Row = 1;
        private  const int POS_Round2Row = 2;
        private  const int POS_TurnInfoRow = 3;
        private const int POS_TurnInfo1RoundNoCol = 1;
        private const int POS_TurnInfo1NoCol = 2;
        private const int POS_TurnInfo1IDCol = 3;
        private const int POS_TurnInfo1TotalNumCol = 4;
        private const int POS_TurnInfo2RoundNoCol = 5;
        private const int POS_TurnInfo2NoCol = 6;
        private const int POS_TurnInfo2IDCol = 7;
        private const int POS_TurnInfo2TotalNumCol = 8;
        private const int POS_EntryTeamInfoRow = 3;
        private  const int POS_EntryTeamInfoEntryIdCol = 9;
        private  const int POS_EntryTeamInfoBibCOl = 10;
        private  const int POS_EntryTeamInfoNameCol = 11;
        private  const int POS_EntryTeamInfoTotalNumCol = 12;

        public void SetGameInfo(int contestId, int gameId)
        {
            ((Excel.Range)Cells[POS_ContestRow, POS_ContestCol]).Value2 = contestId.ToString();
            ((Excel.Range)Cells[POS_GameRow, POS_GameCol]).Value2 = gameId.ToString();
        }

        public bool GetGameInfo(out Guid contestId, out Guid gameId)
        {
            contestId = new Guid();
            gameId = new Guid();
            if (((Excel.Range)Cells[POS_ContestRow, POS_ContestCol]).Value2 != null&&!String.IsNullOrEmpty(((Excel.Range)Cells[POS_ContestRow, POS_ContestCol]).Value2.ToString()))
            {
                contestId = new Guid(((Excel.Range)Cells[POS_ContestRow, POS_ContestCol]).Value2.ToString());
                gameId = new Guid(((Excel.Range)Cells[POS_GameRow, POS_GameCol]).Value2.ToString());
                return true;
            }
            return false;
        }

        public void SetRoundInfo(Guid roundId, int roundNo)
        {
            int row = POS_Round1Row;
            if (roundNo == 2)
            {
                row = POS_Round2Row;
            }
            ((Excel.Range)Cells[row, POS_RoundCol]).Value2 = roundId.ToString();
        }

        public bool GetRoundInfo(int roundNo, out Guid roundId)
        {
            roundId = new Guid();
            int row = POS_Round1Row;
            if (roundNo == 2)
            {
                row = POS_Round2Row;
            }
            if (((Excel.Range)Cells[row, POS_RoundCol]).Value2 != null && !String.IsNullOrEmpty(((Excel.Range)Cells[row, POS_RoundCol]).Value2.ToString()))
            {
                roundId = new Guid(((Excel.Range)Cells[row, POS_RoundCol]).Value2.ToString());
            }
            return false;
        }

        public void ClearTurnInfo()
        {
            ((Excel.Range)Cells[POS_TurnInfoRow, POS_TurnInfo1TotalNumCol]).Value2 = 0;
            ((Excel.Range)Cells[POS_TurnInfoRow, POS_TurnInfo2TotalNumCol]).Value2 = 0;
        }

        public void SetTurnInfo(int roundNo, int no, int turnId)
        {
            int totalNumCol = POS_TurnInfo1TotalNumCol;
            int noCol = POS_TurnInfo1NoCol;
            int roundNoCol = POS_TurnInfo1RoundNoCol;
            int turnIdCol = POS_TurnInfo1IDCol;
            if (roundNo == 2)
            {
                totalNumCol = POS_TurnInfo2TotalNumCol;
                roundNoCol = POS_TurnInfo2RoundNoCol;
                noCol = POS_TurnInfo2NoCol;
                turnIdCol = POS_TurnInfo2IDCol;
            }
            ((Excel.Range)Cells[POS_TurnInfoRow, totalNumCol]).Value2 = no;
            ((Excel.Range)Cells[POS_TurnInfoRow + no, noCol]).Value2 = no;
            ((Excel.Range)Cells[POS_TurnInfoRow + no, turnIdCol]).Value2 = turnId.ToString();
            ((Excel.Range)Cells[POS_TurnInfoRow + no, roundNoCol]).Value2 = roundNo;

        }

        public bool GetTurnInfo(int roundNo, int no, out int turnId)
        {
            turnId = -1;
            int idCol = POS_TurnInfo1IDCol;
            if (roundNo == 2)
            {
                idCol = POS_TurnInfo2IDCol;
            }
            if (no <= GetTurnTotalNo(roundNo))
            {
                turnId = int.Parse(((Excel.Range)Cells[POS_TurnInfoRow + no, idCol]).Value2.ToString());
                return true;
            }
            return false;
        }

        public int GetTurnTotalNo(int roundNo)
        {
            int totalNoCol = POS_TurnInfo1TotalNumCol;
            if (roundNo == 2)
            {
                totalNoCol = POS_TurnInfo2TotalNumCol;
            }
            if (((Excel.Range)Cells[POS_TurnInfoRow, totalNoCol]).Value2 != null && !String.IsNullOrEmpty(((Excel.Range)Cells[POS_TurnInfoRow, totalNoCol]).Value2.ToString()))
            {
                string total = ((Excel.Range)Cells[POS_TurnInfoRow, totalNoCol]).Value2.ToString();
                int totalNo;
                if (Int32.TryParse(total, out totalNo))
                {
                    return totalNo;

                }
            }
            return 0;
        }

        public void ClearEntryTeamInfo()
        {
            ((Excel.Range)Cells[POS_EntryTeamInfoRow, POS_EntryTeamInfoTotalNumCol]).Value2 = 0;
        }

        public void SetEntryTeamInfo(int entryTeamId, int bib, string name)
        {
            int no = IncrementEntryTeamTotalNo();
            ((Excel.Range)Cells[POS_EntryTeamInfoRow + no, POS_EntryTeamInfoEntryIdCol]).Value2 = entryTeamId.ToString();
            ((Excel.Range)Cells[POS_EntryTeamInfoRow + no, POS_EntryTeamInfoBibCOl]).Value2 = bib;
            ((Excel.Range)Cells[POS_EntryTeamInfoRow + no, POS_EntryTeamInfoNameCol]).Value2 = name;
        }

        public bool GetEntryTeamInfo(int bib, string name, out Guid entryTeamId)
        {
            entryTeamId = new Guid();
            int no = GetEntryTeamTotalNo();
            for (int i = 1; i <= no; i++)
            {
                if (((Excel.Range)Cells[POS_EntryTeamInfoRow + i, POS_EntryTeamInfoNameCol]).Value2.ToString().CompareTo(name) == 0)
                {
                    if (((Excel.Range)Cells[POS_EntryTeamInfoRow + i, POS_EntryTeamInfoBibCOl]).Value2.ToString().CompareTo(bib.ToString()) == 0)
                    {
                        entryTeamId = new Guid(((Excel.Range)Cells[POS_EntryTeamInfoRow + i, POS_EntryTeamInfoEntryIdCol]).Value2.ToString());
                        return true;
                    }
                }
            }
            return false;
        }

        public int GetEntryTeamTotalNo()
        {
            int no = 0;
            if (((Excel.Range)Cells[POS_EntryTeamInfoRow, POS_EntryTeamInfoTotalNumCol]).Value2 != null && !String.IsNullOrEmpty(((Excel.Range)Cells[POS_EntryTeamInfoRow, POS_EntryTeamInfoTotalNumCol]).Value2.ToString()))
            {
                string total = ((Excel.Range)Cells[POS_EntryTeamInfoRow, POS_EntryTeamInfoTotalNumCol]).Value2.ToString();
                int totalNo;
                if (Int32.TryParse(total, out totalNo))
                {
                    no = totalNo;
                }
            }
            return no;
        }

        public int IncrementEntryTeamTotalNo()
        {
            int currentNo = GetEntryTeamTotalNo();
            ((Excel.Range)Cells[POS_EntryTeamInfoRow, POS_EntryTeamInfoTotalNumCol]).Value2 = currentNo + 1;
            return currentNo+1;
        }
    }
}
